package yagwl.client.view.fnd;

import yagwl.client.model.MasterDetailDTO;
import yagwl.client.model.fnd.FlexEntityListCache;
import yagwl.client.widget.FormFieldGroup;
import yagwl.client.widget.FormPanel;
import yagwl.client.widget.InputTextBox;
import yagwl.client.widget.MasterDetail;
import yagwl.client.widget.Section;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;

public class FlexEntityView extends Composite {

  public final static String HistoryToken = "flexEntity";
  private final MasterDetail md = new MasterDetail();
  private final FlexEntityListCache cache = new FlexEntityListCache();

  public FlexEntityView() {
    Section rootSP = new Section("Flex Entities");
    HTML h = new HTML(
        "FlexEntity is not a generic widget. It is a composite to create entities and entity attributes");
    rootSP.setDescription(h);
    drawForm();
    rootSP.setContent(md);
    initWidget(rootSP);
  }

  private void drawForm() {
    md.setMasterDetailListCache(cache);
    EntityCreateDialogBox entityCreateDB = new EntityCreateDialogBox();
    md.enableCreateMasterItem("Create Entity", entityCreateDB);
    md.renderUI();
  }
  
  private class EntityCreateDialogBox extends MasterDetail.AbstractMasterItemCreateDialogBox {
    
    public EntityCreateDialogBox() {
      md.super();
      FormPanel form = new FormPanel();
      FormFieldGroup group = new FormFieldGroup();
      final InputTextBox input = new InputTextBox(InputTextBox.DataType.AlphaNumeric, 80);
      final InputTextBox parent = new InputTextBox(InputTextBox.DataType.AlphaNumeric, 80);
      group.addField("Entity Name", input);
      group.addField("Parent Entity", parent, false);
      form.addFieldGroup(group);
      form.addSaveButton("Create", new FormPanel.SaveButtonClickCallback() {
        @Override
        public void save() {
          MasterDetailDTO mdDTO = new MasterDetailDTO();
          mdDTO.setLabel(input.getText());
          cache.createMasterItem(mdDTO);
        }
      });
      form.addCloseDialogBoxButton("Cancel", this);
      form.renderUI();
      setText("Create Entity");
      setWidget(form);
    }

    @Override
    public void setSuccessMessage() {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void setErrorMessage(String errorMessage) {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void clear() {
      // TODO Auto-generated method stub
      
    }
    
  }

}
